home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MONIKER_ / SOURCE__ / MONIKER_.C2 < prev    next >
Text File  |  1992-06-12  |  6KB  |  249 lines

  1. /* Moniker 2.0 */
  2. /* Copyright ⌐1992 by Michael J. Simms */
  3.  
  4. /* Include the program header. */
  5. #include "Moniker 2.0.h"
  6.  
  7. /* Function prototypes. */
  8. OSErr makeAlias(FSSpec);
  9. void initialize(void);
  10. Boolean canRunProgram(void);
  11. void installAEHandlers(void);
  12. void removeAEHandlers(void);
  13. void showExplainBox(void);
  14. short NumToolboxTraps(void);
  15. TrapType GetTrapType(short);
  16. Boolean TrapAvailable(short);
  17. void describeError(short);
  18.  
  19. /* Declarations. */
  20. extern    pascal OSErr HandleODOC(AppleEvent *, AppleEvent *, long);
  21. extern    pascal OSErr HandleQUIT(AppleEvent *, AppleEvent *, long);
  22. extern    pascal OSErr HandleOAPP(AppleEvent *, AppleEvent *, long);
  23. extern    pascal OSErr HandlePDOC(AppleEvent *, AppleEvent *, long);
  24. extern    Boolean    gQuitDemand;
  25.  
  26.  
  27. OSErr
  28. makeAlias(FSSpec fileFSSpec)
  29. /* Create an alias. */
  30. {
  31.     short                aliasRefNum,myResFile;
  32.     OSErr                myErr;
  33.     Point                where={84,50};
  34.     FInfo                aliasFInfo,theFileFInfo;
  35.     Str255                saveString;
  36.     FSSpec                aliasFSSpec;
  37.     AliasHandle            aliasHandle;
  38.     StandardFileReply    reply;
  39.  
  40.     /* Call Standard File Package. */
  41.     GetIndString(saveString,kSaveSTRs,kSaveStr);
  42.     CustomPutFile(saveString,fileFSSpec.name,&reply,kPutFile,where,NIL,NIL,NIL,NIL,NIL);
  43.  
  44.     /* If the user gave the go-ahead then save the file. */
  45.     if (reply.sfGood) {
  46.  
  47.         /* Fill the 'aliasFSSpec' structure. */
  48.         FSMakeFSSpec(reply.sfFile.vRefNum,reply.sfFile.parID,
  49.             reply.sfFile.name,&aliasFSSpec);
  50.  
  51.         /* Create the alias record. */
  52.         myErr=NewAlias(NIL,&fileFSSpec,&aliasHandle);
  53.  
  54.         /* Save the current resource file. */
  55.         myResFile=CurResFile();
  56.  
  57.         /* Get info on the file we are making an alias of. */
  58.         FSpGetFInfo(&fileFSSpec,&theFileFInfo);
  59.  
  60.         /* Create and open the file. */
  61.         FSpCreateResFile(&aliasFSSpec,theFileFInfo.fdCreator,theFileFInfo.fdType,reply.sfScript);
  62.         aliasRefNum=FSpOpenResFile(&aliasFSSpec,fsCurPerm);
  63.  
  64.         /* If possible, write to the file. */
  65.         if (aliasRefNum!=kCantOpen) {
  66.  
  67.             /* Write the resource. */
  68.             UseResFile(aliasRefNum);
  69.             AddResource((Handle)aliasHandle,rAliasType,kAliasRsrcID,aliasFSSpec.name);
  70.             WriteResource((Handle)aliasHandle);
  71.             CloseResFile(aliasRefNum);
  72.  
  73.             /* Set the finder flags. */
  74.             theFileFInfo.fdFlags=kAliasFlag;
  75.             FSpSetFInfo(&aliasFSSpec,&theFileFInfo);
  76.  
  77.         } else describeError(kCantOpen);
  78.  
  79.         /* Set resource file back to this application's. */
  80.         UseResFile(myResFile);
  81.     }
  82. }
  83.  
  84.  
  85. void
  86. initialize(void)
  87. /* Initialization routine. */
  88. {
  89.     MaxApplZone();
  90.     FlushEvents(everyEvent,0);
  91.  
  92.     /* Initialize stuff. */
  93.     InitGraf(&thePort);
  94.     InitFonts();
  95.     InitWindows();
  96.     InitMenus();
  97.     TEInit();
  98.     InitDialogs(NIL);
  99.     InitCursor();
  100.  
  101.     /* Draw the menu bar. */
  102.     DrawMenuBar();
  103. }
  104.  
  105.  
  106. Boolean
  107. canRunProgram(void)
  108. /* Returns TRUE if the program can run in the current environment. */
  109. {
  110.     long    myErr,response;
  111.     Boolean    haveGestalt,haveAliasMgr=FALSE,haveAppleEvents=FALSE,canRun;
  112.  
  113.     /* Get information on the current system setup. */
  114.     if (haveGestalt=TrapAvailable(_GestaltDispatch)) {
  115.  
  116.         if (Gestalt(gestaltAliasMgrAttr,&response)==noErr) {
  117.             /* The default for 'haveAliasMgr' is FALSE. */
  118.             if (BitTst(&response,31-gestaltAliasMgrPresent)) haveAliasMgr=TRUE;
  119.         }
  120.  
  121.         if (Gestalt(gestaltAppleEventsAttr,&response)==noErr) {
  122.             /* The default for 'haveAppleEvents' is FALSE. */
  123.             if (BitTst(&response,31-gestaltAppleEventsPresent)) haveAppleEvents=TRUE;
  124.         }
  125.  
  126.         canRun=haveAliasMgr&&haveAppleEvents;
  127.     }
  128.  
  129.     /* Return TRUE if we can run this program. */
  130.     return(canRun);
  131. }
  132.  
  133.  
  134. void
  135. installAEHandlers(void)
  136. /* Installs Apple Event handlers. */
  137. {
  138.     AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  139.         (EventHandlerProcPtr)HandleODOC,0,FALSE);
  140.     AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  141.         (EventHandlerProcPtr)HandleQUIT,0,FALSE);
  142.     AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,
  143.         (EventHandlerProcPtr)HandlePDOC,0,FALSE);
  144.     AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  145.         (EventHandlerProcPtr)HandleOAPP,0,FALSE);
  146. }
  147.  
  148.  
  149. void
  150. removeAEHandlers(void)
  151. /* Removes Apple Event Handlers. */
  152. {
  153.     AERemoveEventHandler(kCoreEventClass,kAEOpenDocuments,
  154.         (EventHandlerProcPtr)HandleODOC,FALSE);
  155.     AERemoveEventHandler(kCoreEventClass,kAEQuitApplication,
  156.         (EventHandlerProcPtr)HandleQUIT,FALSE);
  157.     AERemoveEventHandler(kCoreEventClass,kAEPrintDocuments,
  158.         (EventHandlerProcPtr)HandlePDOC,FALSE);
  159.     AERemoveEventHandler(kCoreEventClass,kAEOpenApplication,
  160.         (EventHandlerProcPtr)HandleOAPP,FALSE);
  161. }
  162.  
  163.  
  164. void
  165. showExplainBox(void)
  166. /* Shows the dialog explaining the purpose of this program. */
  167. {
  168.     Boolean        gotEvent;
  169.     DialogPtr    explainDLOG;
  170.     EventRecord    event;
  171.  
  172.     /* Create the 'explain' dialog. */
  173.     explainDLOG=GetNewDialog(kExplainDLOGid,NIL,(DialogPtr)-1);
  174.     /* Show the 'explain' dialog. */
  175.     DrawDialog(explainDLOG);
  176.     do {
  177.     } while (!(gotEvent=WaitNextEvent(mDownMask+keyDownMask,&event,10L,NIL)));
  178.     /* Dispose of the 'explain' dialog. */
  179.     DisposDialog(explainDLOG);
  180. }
  181.  
  182.  
  183. short
  184. NumToolboxTraps(void)
  185. /* Returns the number of Toolbox traps. */
  186. {
  187.     short    result;
  188.  
  189.     if (NGetTrapAddress(_InitGraf,ToolTrap)==NGetTrapAddress(0xAA6E,ToolTrap))
  190.         result=0x200;
  191.     else result=0x400;
  192.     return(result);
  193. }
  194.  
  195.  
  196. TrapType
  197. GetTrapType(short theTrap)
  198. /* Returns the type of 'theTrap'. */
  199. {
  200.     TrapType    result;
  201.  
  202.     if ((theTrap&kTrapMask)>0) result=ToolTrap;
  203.     else result=OSTrap;
  204.     return(result);
  205. }
  206.  
  207.  
  208. Boolean
  209. TrapAvailable(short theTrap)
  210. /* Returns TRUE if 'theTrap' is available. */
  211. {
  212.     Boolean        result;
  213.     TrapType    theTrapType;
  214.  
  215.     theTrapType=GetTrapType(theTrap);
  216.     if (theTrapType==ToolTrap) {
  217.         theTrap=(theTrap&0x07FF);
  218.         if (theTrap>=NumToolboxTraps()) theTrap=_Unimplemented;
  219.     }
  220.     result=(NGetTrapAddress(theTrap,theTrapType)!=NGetTrapAddress(_Unimplemented,ToolTrap));
  221.     return(result);
  222. }
  223.  
  224.  
  225. void
  226. describeError(short id)
  227. /* Presents the user with an alert box describing the error that was */
  228. /* (hopefully) not my fault. */
  229. {
  230.     Str255 str;
  231.  
  232.     switch(id) {
  233.         /* Error handling for this program. */
  234.         case kCantRun    : GetIndString(str,kErrorSTRs,kNoAliasMgrStr);
  235.             break;
  236.         case kCantOpen    : GetIndString(str,kErrorSTRs,kCantOpenStr);
  237.             break;
  238.         /* Default message. */
  239.         default            : GetIndString(str,kErrorSTRs,kDefaultStr);
  240.             break;
  241.     }
  242.  
  243.     /* Display the error message. */
  244.     ParamText(str,"\p","\p","\p");
  245.     Alert(kErrorALRTid,NIL);
  246.  
  247.     /* Set 'gQuitDemand' to TRUE. */
  248.     gQuitDemand=TRUE;
  249. }